﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using CarlosMisfortune.view.assets;
using CarlosMisfortune.model;
using TileEngine;

namespace CarlosMisfortune.view
{
    class MasterView
    {
        #region Fields
        private GraphicsDeviceManager m_gdm;
        private GraphicsDevice m_graphicsDevice;
        
        private SpriteBatch m_spriteBatch;

        private SoundAssets m_soundAssets;
        private TextureAssets m_textureAssets;
        private FontAssets m_fontAssets;

        private PlayerModel m_player;
        private PlayerView m_playerView;
        #endregion

        #region Properties
        public SoundAssets Sounds
        {
            get { return m_soundAssets; }
        }

        public TextureAssets SpriteSheets
        {
            get { return m_textureAssets; }
        }

        public FontAssets Fonts
        {
            get { return m_fontAssets; }
        }
        #endregion

        #region Constructor
        public MasterView(GraphicsDeviceManager a_gdm)
        {
            m_gdm = a_gdm;
            //Initialize();
        }

        public void Initialize()
        {
            m_gdm.PreferredBackBufferWidth = 1280;
            m_gdm.PreferredBackBufferHeight = 720;
            m_gdm.ApplyChanges();

            m_soundAssets = new SoundAssets();
            m_textureAssets = new TextureAssets();
            m_fontAssets = new FontAssets();
        }
        #endregion

        #region Public Methods
        public void LoadContent(ContentManager a_content, GraphicsDevice a_graphicsDevice)
        {
            m_graphicsDevice = a_graphicsDevice;
            m_spriteBatch = new SpriteBatch(m_graphicsDevice);

            m_textureAssets.LoadContent(a_content);
            m_fontAssets.LoadContent(a_content);
            m_soundAssets.LoadContent(a_content);

            TileMap.Initialize(m_textureAssets.TileSheets);

            //temp
            //should not be here
            m_player = new PlayerModel();
            m_playerView = new PlayerView(a_content, m_player);

            DebugView.PixelTexture = a_content.Load<Texture2D>(@"textures\pixel");

            Camera.WorldRectangle = new Rectangle(0, 0, TileMap.MapWidth * TileMap.TileWidth, TileMap.MapHeight * TileMap.TileHeight);
            Camera.Position = Vector2.Zero;
            Camera.ViewPortWidth = 1280;
            Camera.ViewPortHeight = 720;

            LevelManager.Initialize(a_content, m_player);
            LevelManager.LoadLevel(5);
        }

        public void Update(GameTime a_gameTime)
        {
            //temp!!!
            m_playerView.Update(a_gameTime, m_player);
            
            Camera.Update(m_player.WorldPos);            
            
            DebugView.Update("player", m_player.CollisionRectangle);
        }

        public void DrawText(SpriteFont a_spriteFont, string a_text, Vector2 a_position, Color a_color)
        {
            m_spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            m_spriteBatch.DrawString(a_spriteFont, a_text, a_position, a_color);
            m_spriteBatch.End();
        }

        public void Draw(GameTime a_gameTime)
        {
            m_graphicsDevice.Clear(Color.CornflowerBlue);
            m_spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            
            TileMap.Draw(m_spriteBatch);
            
            m_playerView.Draw(m_spriteBatch, m_player, m_player.Scale);
            
            DebugView.Draw(m_spriteBatch);
            
            m_spriteBatch.End();
        }
        #endregion
    }
}
